<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="along&#39;s blog">
  <meta name="keyword" content="hexo-theme, vuejs">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      ES6 | ALONG
    
  </title>
  <link href="/cdn.along/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="/cdn.along/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="/cdn.along/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
  <script src="/cdn.along/jquery/3.2.1/jquery.min.js"></script>
  <script src="/cdn.along/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="/cdn.along/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="/cdn.along/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


  
  
    
<script src="/js/local-search.js"></script>


<meta name="generator" content="Hexo 5.4.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>ALONG</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">Home</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">Tags</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">Archives</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">About</a>
          
        </li>
      
      
        <li class="menu-item menu-item-search right-list">
    <a role="button" class="popup-trigger">
        <i class="fa fa-search fa-fw"></i>
    </a>
</li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">Home</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">Tags</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">Archives</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">About</a>
            
          </li>
        
      </ul>
    </div>
    
      <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
            <span class="search-icon">
                <i class="fa fa-search"></i>
            </span>
            <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off"
                    placeholder="Please enter your keyword(s) to search." spellcheck="false"
                    type="search" class="search-input">
            </div>
            <span class="popup-btn-close">
                <i class="fa fa-times-circle"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>
    
  </div>
</header>

    <div id="article-banner">
  <h2>ES6</h2>
  <p class="post-date">2020-08-26</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h2 id="ES2015"><a href="#ES2015" class="headerlink" title="ES2015"></a>ES2015</h2><h3 id="块级作用域let、const"><a href="#块级作用域let、const" class="headerlink" title="块级作用域let、const"></a>块级作用域let、const</h3><p>使用let和const定义的值都会产生块级作用域，这两种方式声明的变量只能在所声明的代码块中被访问。<br>const声明的变量是只读的，不可以修改内存地址</p>
<h4 id="块级作用域的应用场景"><a href="#块级作用域的应用场景" class="headerlink" title="块级作用域的应用场景"></a>块级作用域的应用场景</h4><p>循环计数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 块级作用域应用于双重循环的计数器</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i ++) &#123;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i ++) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(i)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 0,1,2  0,1,2  0,1,2</span></span><br><span class="line"><span class="comment">// 两层循环的i不会互相影响，实际开发中这样使用不便于理解维护</span></span><br></pre></td></tr></table></figure>

<p>闭包应用场景</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 循环绑定事件时，使用var定义计数器，最终点击事件打印的i都是最终i的值，因为i存在于全局环境被所有点击事件共用</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i&lt; <span class="number">3</span>; i ++) &#123;</span><br><span class="line">    element[i].onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        alert(i);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="let-var-const-三者的区别"><a href="#let-var-const-三者的区别" class="headerlink" title="let,var,const 三者的区别"></a>let,var,const 三者的区别</h4><ol>
<li>var声明的变量不存在块级作用域，并且存在<strong>变量提升</strong>，在变量声明之前使用可以取到一个undefined的值。</li>
<li>与var不同，let在声明之前使用会报错，这一特性称为<strong>暂时性死区</strong>，这也导致typeof不是绝对安全的操作，这种特性是为了让开发者养成良好的编程习惯，变量一定要在声明之后使用，否则就报错。</li>
<li>在最外层作用域生命的var变量会挂载到全局对象上，let和const声明的变量存在块级作用域，不会挂在到全局对象上。</li>
<li>let变量值可以随意赋值，const的值不允许改变，因此const在定义时必须初始化</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;vara&#x27;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;window.a: &#x27;</span>, <span class="built_in">window</span>.a); <span class="comment">// vara</span></span><br><span class="line"><span class="comment">// 在最外层声明的var变量会挂载到全局对象window上</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> b = <span class="string">&#x27;letb&#x27;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;window.b: &#x27;</span>, <span class="built_in">window</span>.b); <span class="comment">// undefined</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bar</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    c = <span class="string">&#x27;bar_c&#x27;</span>; <span class="comment">// 没有声明的变量直接赋值，不管是在函数内部还是全局，都会挂载到window上</span></span><br><span class="line">    <span class="keyword">var</span> d = <span class="string">&#x27;bar_d&#x27;</span>; <span class="comment">// 函数内部var定义的变量，只在函数作用域中可以被访问</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        e = <span class="string">&#x27;foo_e&#x27;</span>;</span><br><span class="line">        <span class="keyword">var</span> f = <span class="string">&#x27;foo_f&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    foo();</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;e: &#x27;</span>, e); <span class="comment">// foo_e</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;window.e: &#x27;</span>, <span class="built_in">window</span>.e) <span class="comment">// foo_e</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;f: &#x27;</span>, f); <span class="comment">// test.html:24 Uncaught ReferenceError: f is not defined</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">bar()</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;window.c: &#x27;</span>, <span class="built_in">window</span>.c); <span class="comment">// bar_c</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;c: &#x27;</span>, c); <span class="comment">// bar_C</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;window.d: &#x27;</span>, <span class="built_in">window</span>.d); <span class="comment">// undefined</span></span><br><span class="line"><span class="comment">// console.log(&#x27;d: &#x27;, d); // Uncaught ReferenceError: d is not defined</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;e: &#x27;</span>, e); <span class="comment">// foo_e</span></span><br></pre></td></tr></table></figure>

<h3 id="解构（Destructuring）"><a href="#解构（Destructuring）" class="headerlink" title="解构（Destructuring）"></a>解构（Destructuring）</h3><p>按照一定模式，从数组和对象中提取值，对变量进行赋值</p>
<h4 id="对象的解构"><a href="#对象的解构" class="headerlink" title="对象的解构"></a>对象的解构</h4><p>对象解构语法是在赋值对象的左侧使用了对象字面量</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;<span class="attr">name</span>: <span class="string">&#x27;along&#x27;</span>&#125;</span><br><span class="line"><span class="keyword">const</span> &#123;name&#125; = obj; <span class="comment">// 同名变量解构赋值</span></span><br><span class="line"><span class="keyword">const</span> &#123;<span class="attr">name</span>: newName&#125; = obj; <span class="comment">// 不同变量解构赋值</span></span><br><span class="line"><span class="comment">// newName = along</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 解构同时赋默认值</span></span><br><span class="line"><span class="keyword">const</span> &#123;<span class="attr">age</span>: newAge = <span class="number">24</span>&#125; = obj;</span><br><span class="line"><span class="comment">// newAge = 24;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 简化代码</span></span><br><span class="line"><span class="keyword">const</span> &#123;log&#125; = <span class="built_in">console</span>;</span><br></pre></td></tr></table></figure>
<h4 id="数组的解构"><a href="#数组的解构" class="headerlink" title="数组的解构"></a>数组的解构</h4><p>数组解构时，解构作用在数组内部的位置上</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="keyword">const</span> [a, b, c] = arr;</span><br><span class="line"><span class="comment">// a = 1</span></span><br><span class="line"><span class="comment">// b = 2</span></span><br><span class="line"><span class="comment">// c = 3</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> [a, ...b] = arr;</span><br><span class="line"><span class="comment">// a = 1</span></span><br><span class="line"><span class="comment">// b = [2, 3]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 解构时附默认值</span></span><br><span class="line"><span class="keyword">const</span> [a, b, c, d = <span class="number">4</span>] = arr;</span><br><span class="line"><span class="comment">// d = 4</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> path = <span class="string">&#x27;2020/12/20&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> [, month] = path.split(<span class="string">&#x27;/&#x27;</span>);</span><br><span class="line"><span class="comment">// month = 12</span></span><br></pre></td></tr></table></figure>


<h3 id="模板字符串-Template-literals"><a href="#模板字符串-Template-literals" class="headerlink" title="模板字符串 (Template literals)"></a>模板字符串 (Template literals)</h3><p>模板字面量是允许嵌入表达式的字符串字面量。可以使用多行字符串和字符串插值功能</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> str = <span class="string">`</span></span><br><span class="line"><span class="string">    可以支持换行</span></span><br><span class="line"><span class="string">    可以支持插值，使用表达式</span></span><br><span class="line"><span class="string">    <span class="subst">$&#123;name&#125;</span></span></span><br><span class="line"><span class="string">    <span class="subst">$&#123;<span class="number">1</span>+<span class="number">1</span>&#125;</span></span></span><br><span class="line"><span class="string">`</span>   </span><br><span class="line"></span><br><span class="line"><span class="comment">// 模板字符标签函数</span></span><br><span class="line"><span class="comment">// 该用法会返回字符串数组和所有的插入值</span></span><br><span class="line"><span class="built_in">console</span>.log<span class="string">`hello <span class="subst">$&#123;<span class="string">&#x27;along&#x27;</span>&#125;</span> age <span class="subst">$&#123;<span class="number">23</span>&#125;</span>`</span></span><br><span class="line"><span class="comment">// [&#x27;hello &#x27;, &#x27; age &#x27;], &#x27;along&#x27;, 23</span></span><br><span class="line">alert<span class="string">``</span>hello $&#123;<span class="string">&#x27;along&#x27;</span>&#125; age $&#123;<span class="number">23</span>&#125;<span class="string">`</span></span><br><span class="line"><span class="string">// [&#x27;hello &#x27;, &#x27; age &#x27;] </span></span><br></pre></td></tr></table></figure>

<h3 id="字符串拓展方法"><a href="#字符串拓展方法" class="headerlink" title="字符串拓展方法"></a>字符串拓展方法</h3><h4 id="includes、startsWith、endsWith"><a href="#includes、startsWith、endsWith" class="headerlink" title="includes、startsWith、endsWith"></a>includes、startsWith、endsWith</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> str = <span class="string">&#x27;hello along&#x27;</span></span><br><span class="line">includes(<span class="string">&#x27;alo&#x27;</span>) <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line">startsWith(<span class="string">&#x27;he&#x27;</span>) <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line">endsWith(<span class="string">&#x27;along&#x27;</span>) <span class="comment">// true</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="参数默认值（Default-parameters）"><a href="#参数默认值（Default-parameters）" class="headerlink" title="参数默认值（Default parameters）"></a>参数默认值（Default parameters）</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add1</span>(<span class="params">a = <span class="number">2</span></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> a + <span class="number">1</span> </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">add1() <span class="comment">// 3</span></span><br></pre></td></tr></table></figure>

<h3 id="剩余参数"><a href="#剩余参数" class="headerlink" title="剩余参数"></a>剩余参数</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">...args</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(args);</span><br><span class="line">&#125;</span><br><span class="line">add(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br><span class="line"><span class="comment">// [1, 2, 3]</span></span><br></pre></td></tr></table></figure>

<h3 id="展开数组-Spread"><a href="#展开数组-Spread" class="headerlink" title="展开数组 Spread"></a>展开数组 Spread</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="comment">// console.log.apply(console, arr)</span></span><br><span class="line"><span class="built_in">console</span>.log(...arr);</span><br><span class="line"><span class="comment">// 1 2 3</span></span><br></pre></td></tr></table></figure>

<h3 id="箭头函数（Arrow-functions）"><a href="#箭头函数（Arrow-functions）" class="headerlink" title="箭头函数（Arrow functions）"></a>箭头函数（Arrow functions）</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 简化代码</span></span><br><span class="line"><span class="keyword">const</span> add1 = <span class="function"><span class="params">num</span> =&gt;</span> num + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// this指向</span></span><br><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;along&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> people = &#123;</span><br><span class="line">    name: <span class="string">&#x27;peoplename&#x27;</span>,</span><br><span class="line">    fn: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.name);</span><br><span class="line">    &#125;,</span><br><span class="line">    fn2: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">people.fn(); <span class="comment">// peoplename</span></span><br><span class="line">people.fn2(); <span class="comment">// &quot;&quot;  fn2 中的 this 指向 window 而不是 全局作用域的name</span></span><br></pre></td></tr></table></figure>

<p>需要注意的是，箭头含数arguments 指向的对象并不是当前函数所属的argments，而是上级函数的arguments</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">a</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">arguments</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">arguments</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">a(<span class="number">1</span>,<span class="number">2</span>)()</span><br><span class="line"></span><br><span class="line"><span class="comment">// [Arguments] &#123; &#x27;0&#x27;: 1, &#x27;1&#x27;: 2 &#125;</span></span><br><span class="line"><span class="comment">// [Arguments] &#123; &#x27;0&#x27;: 1, &#x27;1&#x27;: 2 &#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="对象字面量增强（Enhanced-object-literals）"><a href="#对象字面量增强（Enhanced-object-literals）" class="headerlink" title="对象字面量增强（Enhanced object literals）"></a>对象字面量增强（Enhanced object literals）</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 对象属性值简略写法</span></span><br><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;along&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">    name,</span><br><span class="line">    <span class="comment">// 函数的简写，注意这种方式同 </span></span><br><span class="line">    <span class="comment">// fn: function() &#123;console.log(this.name)&#125;</span></span><br><span class="line">    <span class="comment">// 其中的this取决于它的调用者</span></span><br><span class="line">    fn() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.name); </span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 计算属性名</span></span><br><span class="line">    [<span class="number">1</span>+<span class="number">2</span>]: <span class="number">3</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="对象扩展方法"><a href="#对象扩展方法" class="headerlink" title="对象扩展方法"></a>对象扩展方法</h3><h4 id="object-assign"><a href="#object-assign" class="headerlink" title="object.assign"></a>object.assign</h4><p>object.assign 将源对象中的属性复制到目标对象中  </p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> source1 = &#123;</span><br><span class="line">    a: <span class="number">1</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> source2 = &#123;</span><br><span class="line">    b: <span class="number">2</span>,</span><br><span class="line">    c: <span class="number">3</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> target = &#123;<span class="attr">b</span>: <span class="number">4</span>&#125;</span><br><span class="line"><span class="keyword">const</span> res = <span class="built_in">Object</span>.assign(target, source1, source2);</span><br><span class="line">res === target <span class="comment">// true  </span></span><br></pre></td></tr></table></figure>
<p>将对象传给函数进行属性操作时，为了避免对原对象修改，在函数内部复制新的对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;<span class="attr">name</span>: <span class="string">&#x27;along&#x27;</span>&#125;;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">tar</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 参数是对象时tar是一个引用地址，指向参数的对象地址</span></span><br><span class="line">    <span class="comment">// 直接操作tar会改变参数的属性</span></span><br><span class="line">    <span class="comment">// tar.name = &#x27;new along&#x27;;</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">const</span> newobj = <span class="built_in">Object</span>.assign(&#123;&#125;, tar);</span><br><span class="line">    newobj.name = <span class="string">&#x27;new along&#x27;</span>;</span><br><span class="line">    <span class="built_in">console</span>.log(newobj);</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">fn(obj); <span class="comment">// &#123;name: &#x27;new along&#x27;&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(obj); <span class="comment">// &#123;name: &#x27;along&#x27;&#125;</span></span><br></pre></td></tr></table></figure>

<p>可以通过Object.assign()为对象参数设置默认值、</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">option</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> defaultparams = &#123;</span><br><span class="line">        page: <span class="number">1</span>,</span><br><span class="line">        pagesize: <span class="number">10</span>,</span><br><span class="line">        method: <span class="string">&#x27;get&#x27;</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> params = <span class="built_in">Object</span>.assign(&#123;&#125;, option, defaultparams);</span><br><span class="line">    <span class="built_in">console</span>.log(params);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> option = &#123;</span><br><span class="line">    method: <span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">    url: <span class="string">&#x27;localhost:8080&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">fn(option)</span><br></pre></td></tr></table></figure>

<h4 id="Object-is"><a href="#Object-is" class="headerlink" title="Object.is()"></a>Object.is()</h4><p>判断两个值是否相等 </p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">0</span> == <span class="literal">false</span> <span class="comment">// true</span></span><br><span class="line"><span class="number">0</span> === <span class="literal">false</span> <span class="comment">// false</span></span><br><span class="line">+<span class="number">0</span> === <span class="number">-0</span> <span class="comment">// true</span></span><br><span class="line"><span class="literal">NaN</span> === <span class="literal">NaN</span> <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.is(+<span class="number">0</span>, <span class="number">-0</span>); <span class="comment">// false   </span></span><br><span class="line"><span class="built_in">Object</span>.is(<span class="literal">NaN</span>, <span class="literal">NaN</span>); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<h3 id="Proxy-代理"><a href="#Proxy-代理" class="headerlink" title="Proxy 代理"></a>Proxy 代理</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> person = &#123;</span><br><span class="line">    name: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">    age: <span class="number">20</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> personProxy = <span class="keyword">new</span> <span class="built_in">Proxy</span>(person, &#123;</span><br><span class="line">    get(target, property) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(target, property);</span><br><span class="line">        <span class="keyword">return</span> property <span class="keyword">in</span> target ? target[property] : <span class="string">&#x27;-&#x27;</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    set(target, property, value) &#123;</span><br><span class="line">        <span class="keyword">if</span> (property === <span class="string">&#x27;age&#x27;</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!<span class="built_in">Number</span>.isInteger(value)) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">`age must be a number but <span class="subst">$&#123;value&#125;</span> [<span class="subst">$&#123;<span class="keyword">typeof</span> value&#125;</span>]`</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;   </span><br><span class="line">        target[property] = value;</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(personProxy.name);</span><br><span class="line"><span class="comment">// &#123; name: &#x27;along&#x27;, age: 20 &#125; name</span></span><br><span class="line"><span class="comment">// along</span></span><br><span class="line"><span class="built_in">console</span>.log(personProxy.bb);</span><br><span class="line"><span class="comment">// &#123; name: &#x27;along&#x27;, age: 20 &#125; bb</span></span><br><span class="line"><span class="comment">// -</span></span><br><span class="line">personProxy.age = <span class="string">&#x27;20&#x27;</span></span><br><span class="line"><span class="comment">// throw new TypeError(`age must be a number but $&#123;value&#125; [$&#123;typeof value&#125;]`);</span></span><br><span class="line"><span class="comment">// ^</span></span><br><span class="line"><span class="comment">// TypeError: age must be a number but 20 [string]</span></span><br></pre></td></tr></table></figure>

<p>vue 3.0 开始使用proxy 实现内部数据的相应</p>
<h4 id="Proxy-与-Object-defineProperty-比较"><a href="#Proxy-与-Object-defineProperty-比较" class="headerlink" title="Proxy 与 Object.defineProperty 比较"></a>Proxy 与 Object.defineProperty 比较</h4><p>defineProperty  只能监视对象的读取和写入 Proxy 可以监视一些 defineProperty  监视不到的行为</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> person = &#123;</span><br><span class="line">    name: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">    age: <span class="number">20</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> personProxy = <span class="keyword">new</span> <span class="built_in">Proxy</span>(person, &#123;</span><br><span class="line">     deleteProperty(target, property) &#123;</span><br><span class="line">         <span class="built_in">console</span>.log(<span class="string">&#x27;delete&#x27;</span>, property);</span><br><span class="line">         <span class="keyword">delete</span> target[property];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">delete</span> personProxy.name;</span><br><span class="line"><span class="built_in">console</span>.log(personProxy);</span><br><span class="line"><span class="comment">// delete name</span></span><br><span class="line"><span class="comment">// &#123; age: 20 &#125;</span></span><br></pre></td></tr></table></figure>

<p>除了删除操作之外，可操作的行为见下表</p>
<img src="/2020/08/26/ES6/clipboard.png" class="">

<h4 id="借助Proxy监视数组操作"><a href="#借助Proxy监视数组操作" class="headerlink" title="借助Proxy监视数组操作"></a>借助Proxy监视数组操作</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> list = []</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> listProxy = <span class="keyword">new</span> <span class="built_in">Proxy</span>(list, &#123;</span><br><span class="line">    set(target, property, value) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(target, property, value); </span><br><span class="line">        target[property] = value;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>; <span class="comment">// 需要返回true表示操作成功</span></span><br><span class="line">    &#125;,</span><br><span class="line">    deleteProperty(target, property) &#123;</span><br><span class="line">         <span class="built_in">console</span>.log(<span class="string">&#x27;delete&#x27;</span>, property);</span><br><span class="line">         <span class="keyword">delete</span> target[property];</span><br><span class="line">         <span class="keyword">return</span> <span class="literal">true</span>; <span class="comment">// 需要返回true表示操作成功</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line">listProxy.push(<span class="number">1</span>); <span class="comment">// push 操作会至少触发两次set</span></span><br><span class="line"><span class="comment">// [] 0 1</span></span><br><span class="line"><span class="comment">// [ 1 ] length 1</span></span><br><span class="line">listProxy.push(<span class="number">2</span>);</span><br><span class="line"><span class="comment">// [ 1 ] 1 2</span></span><br><span class="line"><span class="comment">// [ 1, 2 ] length 2</span></span><br><span class="line">listProxy.push(<span class="number">3</span>);</span><br><span class="line"><span class="comment">// [ 1, 2 ] 2 3</span></span><br><span class="line"><span class="comment">// [ 1, 2, 3 ] length 3</span></span><br><span class="line">listProxy.push(<span class="number">4</span>);</span><br><span class="line"><span class="comment">// [ 1, 2, 3 ] 3 4</span></span><br><span class="line"><span class="comment">// [ 1, 2, 3, 4 ] length 4</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 这里执行到shift时，会首先出发三次set，讲数组最后的三个元素前移，</span></span><br><span class="line"><span class="comment">// 然后触发删除操作</span></span><br><span class="line"><span class="comment">// 最后再次触发 set 的操作修改length</span></span><br><span class="line">listProxy.shift(); </span><br><span class="line"><span class="comment">// [ 1, 2, 3, 4 ] 0 2</span></span><br><span class="line"><span class="comment">// [ 2, 2, 3, 4 ] 1 3</span></span><br><span class="line"><span class="comment">// [ 2, 3, 3, 4 ] 2 4</span></span><br><span class="line"><span class="comment">// delete 3</span></span><br><span class="line"><span class="comment">// [ 2, 3, 4, &lt;1 empty item&gt; ] length 3</span></span><br></pre></td></tr></table></figure>
<h4 id="Proxy-以非侵入的方式监管对象读写"><a href="#Proxy-以非侵入的方式监管对象读写" class="headerlink" title="Proxy 以非侵入的方式监管对象读写"></a>Proxy 以非侵入的方式监管对象读写</h4><p>defineProperty 需要通过监听特定的属性，如</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> person = &#123;<span class="attr">name</span>: <span class="string">&#x27;along&#x27;</span>&#125;;</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(person, <span class="string">&#x27;name&#x27;</span>, &#123;</span><br><span class="line">    get() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;get&#x27;</span>);</span><br><span class="line">        <span class="keyword">return</span> person._name;</span><br><span class="line">    &#125;,</span><br><span class="line">    set(value) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;name 被修改&#x27;</span>);</span><br><span class="line">        person._name = value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(person.name);</span><br><span class="line"><span class="comment">// undefined</span></span><br><span class="line">person.name = <span class="string">&#x27;along&#x27;</span>;</span><br><span class="line"><span class="comment">// name 被修改</span></span><br><span class="line"><span class="built_in">console</span>.log(person)</span><br><span class="line"><span class="comment">// &#123; name: [Getter/Setter], _name: &#x27;along&#x27; &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(person.name)</span><br><span class="line"><span class="comment">// get</span></span><br><span class="line"><span class="comment">// along</span></span><br></pre></td></tr></table></figure>

<p>从以上代码可以看出，defineProperty方式会修改被监控的对象本身，读取写入操作需要借助辅助内存空间来保存真实值</p>
<h3 id="Reflect"><a href="#Reflect" class="headerlink" title="Reflect"></a>Reflect</h3><p>静态类，不能用new操作符<br>Reflect 内部封装了一系列对对象的底层操作<br>Reflect 成员方法就是Proxy处理对象的默认实现</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">const</span> person = &#123;</span><br><span class="line">    name: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">    age: <span class="number">23</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> personProxy = <span class="keyword">new</span> <span class="built_in">Proxy</span>(person, &#123;</span><br><span class="line">    <span class="comment">// 在不设置 get 方法时，相当于返回 Reflect.get(target, property);</span></span><br><span class="line">    get (target, property) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;get&#x27;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Reflect</span>.get(target, property);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(personProxy.name)</span><br></pre></td></tr></table></figure>

<h4 id="统一操作对象的API"><a href="#统一操作对象的API" class="headerlink" title="统一操作对象的API"></a>统一操作对象的API</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> person = &#123;</span><br><span class="line">    name: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">    age: <span class="number">24</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">(<span class="built_in">Reflect</span>.has(person, <span class="string">&#x27;name&#x27;</span>)</span><br><span class="line"><span class="comment">// true  相当于  &#x27;name&#x27; in person</span></span><br><span class="line"><span class="built_in">Reflect</span>.deleteProperty(person, <span class="string">&#x27;name&#x27;</span>)</span><br><span class="line"><span class="comment">// 相当于 delete person[&#x27;name&#x27;] | delete person.name</span></span><br><span class="line"><span class="built_in">Reflect</span>.ownKeys(person);</span><br><span class="line"><span class="comment">// [&#x27;name&#x27;, &#x27;age&#x27;]  相当于  person.keys();</span></span><br></pre></td></tr></table></figure>

<h3 id="Classes-类"><a href="#Classes-类" class="headerlink" title="Classes 类"></a>Classes 类</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(name) &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    say () &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span> say`</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> person = <span class="keyword">new</span> Person(<span class="string">&#x27;along&#x27;</span>);</span><br><span class="line">person.say();</span><br><span class="line"><span class="comment">// along say</span></span><br></pre></td></tr></table></figure>

<h4 id="静态成员"><a href="#静态成员" class="headerlink" title="静态成员"></a>静态成员</h4><ul>
<li>实例方法（通过实例调用）实例方法中的this指向实例</li>
<li>静态方法（通过类本身调用）静态方法中的this指向类本身</li>
</ul>
<h4 id="static关键字"><a href="#static关键字" class="headerlink" title="static关键字"></a>static关键字</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(name) &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    say () &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>)</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span> say`</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">static</span> create (name) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Person(name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> person = Person.create(<span class="string">&#x27;along&#x27;</span>);</span><br><span class="line"><span class="comment">// [Function: Person]</span></span><br><span class="line">person.say();</span><br><span class="line"><span class="comment">// Person &#123; name: &#x27;along&#x27; &#125;</span></span><br><span class="line"><span class="comment">// along say</span></span><br></pre></td></tr></table></figure>

<h3 id="继承-extends"><a href="#继承-extends" class="headerlink" title="继承 extends"></a>继承 extends</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(name) &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    say () &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>)</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span> say`</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">static</span> create (name) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Person(name);</span><br><span class="line">        <span class="comment">// return new this(name); 可以使用new this , </span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> <span class="keyword">extends</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(name, schoolname)&#123;</span><br><span class="line">        <span class="built_in">super</span>(name);</span><br><span class="line">        <span class="built_in">this</span>.schoolname = schoolname;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    welcome() &#123;</span><br><span class="line">        <span class="built_in">super</span>.say();</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">`from <span class="subst">$&#123;<span class="built_in">this</span>.schoolname&#125;</span>`</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> along = <span class="keyword">new</span> Student(<span class="string">&#x27;along&#x27;</span>, <span class="string">&#x27;黑科技&#x27;</span>);</span><br><span class="line">along.welcome()</span><br><span class="line"><span class="comment">// Student &#123; name: &#x27;along&#x27;, schoolname: &#x27;黑科技&#x27; &#125;</span></span><br><span class="line"><span class="comment">// along say</span></span><br><span class="line"><span class="comment">// from 黑科技</span></span><br><span class="line"><span class="built_in">console</span>.log(Student.create(<span class="string">&#x27;along&#x27;</span>));</span><br><span class="line"><span class="comment">// [Function: Student]                  Student继承的Person的静态方法，this指向Student类</span></span><br><span class="line"><span class="comment">// Person &#123; name: &#x27;along&#x27; &#125;            </span></span><br></pre></td></tr></table></figure>

<h3 id="Set数据结构"><a href="#Set数据结构" class="headerlink" title="Set数据结构"></a>Set数据结构</h3><p>不重复元素的集合</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> s = <span class="keyword">new</span> <span class="built_in">Set</span>();</span><br><span class="line">s.add(<span class="number">1</span>).add(<span class="number">2</span>).add(<span class="number">2</span>);</span><br><span class="line"><span class="built_in">console</span>.log(s);</span><br><span class="line"><span class="comment">// Set &#123; 1, 2&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 遍历 不能用 for in, 不能用下标取值</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> i <span class="keyword">of</span> s) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(i);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 或者 forEach</span></span><br><span class="line">s.forEach(<span class="function"><span class="params">l</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(l);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取 Set 长度</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;size&#x27;</span>, s.size);</span><br><span class="line"><span class="comment">// size 2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 判断是否存在某个元素</span></span><br><span class="line"><span class="built_in">console</span>.log(s.has(<span class="number">100</span>));</span><br><span class="line"><span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 删除某个元素，删除成功返回true，失败即本来不存在返回false</span></span><br><span class="line"><span class="built_in">console</span>.log(s.delete(<span class="number">3</span>));</span><br><span class="line"><span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 清空</span></span><br><span class="line">s.clear();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 数组去重</span></span><br><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line"><span class="keyword">const</span> result = <span class="built_in">Array</span>.from(<span class="keyword">new</span> <span class="built_in">Set</span>(arr)); <span class="comment">// [...new Set(arr)]</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(result);</span><br><span class="line"><span class="comment">// [1,2,3,4]</span></span><br></pre></td></tr></table></figure>

<h3 id="Map-数据结构，映射两个任意类型数据之间的关系"><a href="#Map-数据结构，映射两个任意类型数据之间的关系" class="headerlink" title="Map 数据结构，映射两个任意类型数据之间的关系"></a>Map 数据结构，映射两个任意类型数据之间的关系</h3><p>可以使用任何数据作为键，普通对象只能用字符串作为键</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> along = &#123;<span class="attr">name</span>: <span class="string">&#x27;along&#x27;</span>&#125;;</span><br><span class="line"><span class="keyword">const</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line"></span><br><span class="line">map.set(along, <span class="number">25</span>);</span><br><span class="line"><span class="built_in">console</span>.log(map)</span><br><span class="line"><span class="comment">// Map &#123; &#123; name: &#x27;along&#x27; &#125; =&gt; 25 &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(map.get(along))</span><br><span class="line"><span class="comment">// 25</span></span><br><span class="line">map.has(along); <span class="comment">// 查看是否存在键  返回true</span></span><br><span class="line">map.delete(along); <span class="comment">// 返回删除结果</span></span><br><span class="line"><span class="comment">// true</span></span><br><span class="line">map.clear() <span class="comment">// 清空</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 遍历 forEach 获取 for of </span></span><br><span class="line">map.forEach(<span class="function">(<span class="params">value, key</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value, key)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> [key, value] <span class="keyword">of</span> map)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(key, value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Symbol-第七个数据类型，-esmascript-2017-第八个数据类型BigInt"><a href="#Symbol-第七个数据类型，-esmascript-2017-第八个数据类型BigInt" class="headerlink" title="Symbol  第七个数据类型，(esmascript 2017 第八个数据类型BigInt)"></a>Symbol  第七个数据类型，(esmascript 2017 第八个数据类型BigInt)</h3><p>可以避免属性名冲突，为对象定义独一无二的属性名</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Symbol</span>() === <span class="built_in">Symbol</span>() <span class="comment">// false </span></span><br><span class="line"><span class="keyword">typeof</span> <span class="built_in">Symbol</span>(); <span class="comment">// symbol</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;[<span class="built_in">Symbol</span>()]: <span class="number">123</span>&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(obj);</span><br><span class="line"><span class="comment">// &#123; [Symbol()]: 123 &#125;</span></span><br></pre></td></tr></table></figure>

<p>用于模拟对象私有成员,在对象内部由于缓存了Symbol的索引，可以对Symbol属性进行操作，在对象外部由于拿不到Symbol的索引所以无法对属性访问</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="built_in">Symbol</span>();</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> person = &#123;</span><br><span class="line">    [name]: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">    say() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>[name]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>全局使用相同的Symbol时,可以通过Symbol.for() 传入字符串，Symbol内部维护了字符串和Symbol的对应关系。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> s1 = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;foo&#x27;</span>); <span class="comment">// 相当于  const s1 = Symbol(); // const s2 = s1;</span></span><br><span class="line"><span class="keyword">const</span> s2 = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;foo&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(s1 === s2)</span><br><span class="line"><span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> strtrue = <span class="built_in">Symbol</span>(<span class="string">&#x27;true&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> booltrue = <span class="built_in">Symbol</span>(<span class="literal">true</span>);</span><br><span class="line">strtrue === booltrue <span class="comment">// true;</span></span><br></pre></td></tr></table></figure>

<p>Symbol内置常量</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">    [<span class="built_in">Symbol</span>.toStringTag]: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(obj.toString())</span><br><span class="line"><span class="comment">// [object along]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>获取Symbol属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Symbol 属性通过    for in ，Object.keys(), JSON.stringify(), 都会被忽略</span></span><br><span class="line"><span class="comment">// 可以通过  getOwnPropertySymbols 获取Symbol的keys</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">    [<span class="built_in">Symbol</span>(<span class="string">&#x27;name&#x27;</span>)]: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getOwnPropertySymbols(obj));</span><br><span class="line"><span class="comment">// [ Symbol(name) ]</span></span><br></pre></td></tr></table></figure>

<h3 id="for-of-循环，"><a href="#for-of-循环，" class="headerlink" title="for of 循环，"></a>for of 循环，</h3><p>可以作为遍历所有数据结构的统一方式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> item <span class="keyword">of</span> arr) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(item);</span><br><span class="line">    <span class="keyword">if</span> (item === <span class="number">2</span>) <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// for of 循环可以通过 break 跳出循环  ， forEach  map 等方式无法跳出循环， 需要跳出循环需要通过arr.some，arr.every方法</span></span><br></pre></td></tr></table></figure>

<p>Map对象通过for of 遍历返回数组，第一个值是键，第二个值是值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line">map.set(&#123;<span class="attr">name</span>: <span class="string">&#x27;along&#x27;</span>&#125;, <span class="number">25</span>);</span><br><span class="line">map.set(<span class="string">&#x27;school&#x27;</span>, <span class="string">&#x27;黑科&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> [key, value] <span class="keyword">of</span> map) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(key, value)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// &#123; name: &#x27;along&#x27; &#125; 25</span></span><br><span class="line"><span class="comment">// school 黑科</span></span><br></pre></td></tr></table></figure>

<p>通过for of 遍历对象， 会发现报错</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> person = &#123;</span><br><span class="line">    name: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">    age: <span class="number">24</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> person) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// TypeError: person is not iterable</span></span><br></pre></td></tr></table></figure>


<p>通过浏览器控制台打印数组、Set、 Map 等对象会发现，他们的原型上都存在 [Symbol(Symbol.iterator)]属性</p>
<img src="/2020/08/26/ES6/iterator.png" class="">

<p>通过调用iterator查看结果，这里以数组为例</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> iterator = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>][<span class="built_in">Symbol</span>.iterator]();</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="comment">// &#123; value: 1, done: false &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="comment">// &#123; value: 2, done: false &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="comment">// &#123; value: 3, done: false &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="comment">// &#123; value: undefined, done: true &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>实现可迭代接口 iterable</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> iterable = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> keys = <span class="built_in">Object</span>.keys(<span class="built_in">this</span>);</span><br><span class="line">    <span class="keyword">let</span> index = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">const</span> self = <span class="built_in">this</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        <span class="comment">// iterator</span></span><br><span class="line">        next: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="comment">// iterationResult</span></span><br><span class="line">            <span class="keyword">const</span> iterationResult = &#123;</span><br><span class="line">                value: keys[index],</span><br><span class="line">                done: index &gt;= keys.length,</span><br><span class="line">            &#125;</span><br><span class="line">            index ++;</span><br><span class="line">            <span class="keyword">return</span> iterationResult;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> person = &#123;</span><br><span class="line">    name: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">    age: <span class="number">25</span>,</span><br><span class="line">    <span class="comment">// 实现iterable 接口</span></span><br><span class="line">    [<span class="built_in">Symbol</span>.iterator]: iterable</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> person) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="迭代器模式-让用户通过特定的接口访问容器的数据，不需要了解容器内部的数据结构。"><a href="#迭代器模式-让用户通过特定的接口访问容器的数据，不需要了解容器内部的数据结构。" class="headerlink" title="迭代器模式 让用户通过特定的接口访问容器的数据，不需要了解容器内部的数据结构。"></a>迭代器模式 让用户通过特定的接口访问容器的数据，不需要了解容器内部的数据结构。</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// a.js</span></span><br><span class="line"><span class="keyword">const</span> todos = &#123;</span><br><span class="line">    life: [<span class="string">&#x27;工作&#x27;</span>, <span class="string">&#x27;坐公交&#x27;</span>],</span><br><span class="line">    learn: [<span class="string">&#x27;ES6&#x27;</span>, <span class="string">&#x27;typescript&#x27;</span>],</span><br><span class="line"></span><br><span class="line">    [<span class="built_in">Symbol</span>.iterator]: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">const</span> keys = [].concat(<span class="built_in">this</span>.life, <span class="built_in">this</span>.learn);</span><br><span class="line">        <span class="keyword">let</span> index = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            next: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">                <span class="keyword">const</span> iterationResult = &#123;</span><br><span class="line">                    value: keys[index],</span><br><span class="line">                    done: index &gt;= keys.length,</span><br><span class="line">                &#125;</span><br><span class="line">                index ++;</span><br><span class="line">                <span class="keyword">return</span> iterationResult;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// b.js</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> todos) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="生成器-Generator"><a href="#生成器-Generator" class="headerlink" title="生成器 Generator"></a>生成器 Generator</h3><p>解决异步函数嵌套问题，从而提供更好的异步编程解决方案</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 生成器函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> * <span class="title">fn</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;along&#x27;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">100</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> gen = fn();</span><br><span class="line"><span class="built_in">console</span>.log(gen);</span><br><span class="line"><span class="comment">// Object [Generator] &#123;&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(gen.next())</span><br><span class="line"><span class="comment">// along</span></span><br><span class="line"><span class="comment">// &#123; value: 100, done: true &#125;</span></span><br></pre></td></tr></table></figure>
<p>生成器函数可以自动返回一个生成器对象，生成器函数内部的逻辑会惰性执行</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> * <span class="title">fn</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;11&#x27;</span>);</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">100</span>;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;22&#x27;</span>);</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">200</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> gen = fn();</span><br><span class="line"><span class="built_in">console</span>.log(gen.next())</span><br><span class="line"><span class="comment">// 11</span></span><br><span class="line"><span class="comment">// &#123; value: 100, done: false &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(gen.next())</span><br><span class="line"><span class="comment">// 22</span></span><br><span class="line"><span class="comment">// &#123; value: 200, done: false &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(gen.next())</span><br><span class="line"><span class="comment">// &#123; value: undefined, done: true &#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="生成器应用"><a href="#生成器应用" class="headerlink" title="生成器应用"></a>生成器应用</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 发号器</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> * <span class="title">fn</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> id = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span>(<span class="literal">true</span>) &#123;</span><br><span class="line">        <span class="keyword">yield</span> id ++;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> gen = fn();</span><br><span class="line"><span class="built_in">console</span>.log(gen.next())</span><br><span class="line"><span class="built_in">console</span>.log(gen.next())</span><br><span class="line"><span class="built_in">console</span>.log(gen.next())</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>使用生成器简化迭代器</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// a.js</span></span><br><span class="line"><span class="keyword">const</span> todos = &#123;</span><br><span class="line">    life: [<span class="string">&#x27;工作&#x27;</span>, <span class="string">&#x27;坐公交&#x27;</span>],</span><br><span class="line">    learn: [<span class="string">&#x27;ES6&#x27;</span>, <span class="string">&#x27;typescript&#x27;</span>],</span><br><span class="line"></span><br><span class="line">    <span class="comment">// [Symbol.iterator]: function () &#123;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//     const keys = [].concat(this.life, this.learn);</span></span><br><span class="line">    <span class="comment">//     let index = 0;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//     return &#123;</span></span><br><span class="line">    <span class="comment">//         next: function () &#123;</span></span><br><span class="line">    <span class="comment">//             const iterationResult = &#123;</span></span><br><span class="line">    <span class="comment">//                 value: keys[index],</span></span><br><span class="line">    <span class="comment">//                 done: index &gt;= keys.length,</span></span><br><span class="line">    <span class="comment">//             &#125;</span></span><br><span class="line">    <span class="comment">//             index ++;</span></span><br><span class="line">    <span class="comment">//             return iterationResult;</span></span><br><span class="line">    <span class="comment">//         &#125;</span></span><br><span class="line">    <span class="comment">//     &#125;</span></span><br><span class="line">    <span class="comment">// &#125;,</span></span><br><span class="line">    [<span class="built_in">Symbol</span>.iterator]: <span class="function"><span class="keyword">function</span> * (<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">const</span> keys = [].concat(<span class="built_in">this</span>.life, <span class="built_in">this</span>.learn);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> item <span class="keyword">of</span> keys) &#123;</span><br><span class="line">            <span class="keyword">yield</span> item;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// b.js</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> todos) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="ES2016"><a href="#ES2016" class="headerlink" title="ES2016"></a>ES2016</h2><h3 id="数组方法includs"><a href="#数组方法includs" class="headerlink" title="数组方法includs"></a>数组方法includs</h3><p>在ES2016之前需要通过indexOf 返回的下表来判断，但是这种方式也存在一些问题，比如不能查找数组中的NaN</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="string">&#x27;foo&#x27;</span>, <span class="number">1</span>, <span class="literal">NaN</span>, <span class="literal">false</span>];</span><br><span class="line"></span><br><span class="line">arr.indexOf(<span class="string">&#x27;foo&#x27;</span>);</span><br><span class="line"><span class="comment">// 0</span></span><br><span class="line">arr.indexOf(<span class="string">&#x27;b&#x27;</span>);</span><br><span class="line"><span class="comment">// -1</span></span><br><span class="line">arr.indexOf(<span class="literal">NaN</span>);</span><br><span class="line"><span class="comment">// -1</span></span><br><span class="line"></span><br><span class="line">arr.includes(<span class="string">&#x27;foo&#x27;</span>);</span><br><span class="line"><span class="comment">// true</span></span><br><span class="line">arr.includes(<span class="literal">NaN</span>);</span><br><span class="line"><span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<h3 id="指数运算符"><a href="#指数运算符" class="headerlink" title="指数运算符"></a>指数运算符</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Math</span>.pow(<span class="number">2</span>, <span class="number">10</span>);</span><br><span class="line"><span class="comment">// 1024</span></span><br><span class="line"><span class="number">2</span> ** <span class="number">10</span>;</span><br><span class="line"><span class="comment">// 1024</span></span><br></pre></td></tr></table></figure>

<h2 id="ES2017"><a href="#ES2017" class="headerlink" title="ES2017"></a>ES2017</h2><p>Object.values 返回对象的值数组<br>Object.entries 返回对象键值对数组<br>Object.getOwnPropertyDescriptors 获取属性描述符，可以通过拷贝属性描述符拷贝getter、setter<br>padEnd 、 padStart  使用指定字符串填充字符串使其达到目标长度<br>参数列表尾逗号， 便于代码修改时参数扩充<br>Async/Await  Promise语法糖</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> person = &#123;</span><br><span class="line">    name: <span class="string">&#x27;along&#x27;</span>,</span><br><span class="line">    age: <span class="string">&#x27;24&#x27;</span>,</span><br><span class="line">    [<span class="built_in">Symbol</span>()]: <span class="number">123</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">Object</span>.values(person);</span><br><span class="line"><span class="comment">// [ &#x27;along&#x27;, &#x27;24&#x27; ]</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.entries(person)</span><br><span class="line"><span class="comment">// [ [ &#x27;name&#x27;, &#x27;along&#x27; ], [ &#x27;age&#x27;, &#x27;24&#x27; ] ]</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.entries([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]);</span><br><span class="line"><span class="comment">// [ [ &#x27;0&#x27;, 1 ], [ &#x27;1&#x27;, 2 ], [ &#x27;2&#x27;, 3 ] ]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> description = <span class="built_in">Object</span>.getOwnPropertyDescriptors(person);</span><br><span class="line"><span class="comment">// &#123;</span></span><br><span class="line"><span class="comment">//   name: &#123;</span></span><br><span class="line"><span class="comment">//     value: &#x27;along&#x27;,</span></span><br><span class="line"><span class="comment">//     writable: true,</span></span><br><span class="line"><span class="comment">//     enumerable: true,</span></span><br><span class="line"><span class="comment">//     configurable: true</span></span><br><span class="line"><span class="comment">//   &#125;,</span></span><br><span class="line"><span class="comment">//   age: &#123; value: &#x27;24&#x27;, writable: true, enumerable: true, configurable: true &#125;,</span></span><br><span class="line"><span class="comment">//   [Symbol()]: &#123; value: 123, writable: true, enumerable: true, configurable: true &#125;</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 填充字符串</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> [key, value] <span class="keyword">of</span> <span class="built_in">Object</span>.entries(person)) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;key.padEnd(<span class="number">16</span>, <span class="string">&#x27;-&#x27;</span>)&#125;</span>|<span class="subst">$&#123;value.padStart(<span class="number">10</span>, <span class="string">&#x27;-&#x27;</span>)&#125;</span>`</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// name------------|-----along</span></span><br><span class="line"><span class="comment">// age-------------|--------24</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 参数列表尾逗号</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">    name,</span></span></span><br><span class="line"><span class="function"><span class="params">    age,</span></span></span><br><span class="line"><span class="function"><span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#notes" >
    <span class="tag-code">notes</span>
  </a>

  <a href="/tags#javascript" >
    <span class="tag-code">javascript</span>
  </a>

  <a href="/tags#es6" >
    <span class="tag-code">es6</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
    
      <a class="nav-right" href="/2020/08/26/JS%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B/">
        
          JS异步编程
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- No Comment -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#ES2015"><span class="toc-nav-text">ES2015</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%9D%97%E7%BA%A7%E4%BD%9C%E7%94%A8%E5%9F%9Flet%E3%80%81const"><span class="toc-nav-text">块级作用域let、const</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E5%9D%97%E7%BA%A7%E4%BD%9C%E7%94%A8%E5%9F%9F%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-nav-text">块级作用域的应用场景</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#let-var-const-%E4%B8%89%E8%80%85%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-nav-text">let,var,const 三者的区别</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E8%A7%A3%E6%9E%84%EF%BC%88Destructuring%EF%BC%89"><span class="toc-nav-text">解构（Destructuring）</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E5%AF%B9%E8%B1%A1%E7%9A%84%E8%A7%A3%E6%9E%84"><span class="toc-nav-text">对象的解构</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E6%95%B0%E7%BB%84%E7%9A%84%E8%A7%A3%E6%9E%84"><span class="toc-nav-text">数组的解构</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E6%A8%A1%E6%9D%BF%E5%AD%97%E7%AC%A6%E4%B8%B2-Template-literals"><span class="toc-nav-text">模板字符串 (Template literals)</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%93%E5%B1%95%E6%96%B9%E6%B3%95"><span class="toc-nav-text">字符串拓展方法</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#includes%E3%80%81startsWith%E3%80%81endsWith"><span class="toc-nav-text">includes、startsWith、endsWith</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%8F%82%E6%95%B0%E9%BB%98%E8%AE%A4%E5%80%BC%EF%BC%88Default-parameters%EF%BC%89"><span class="toc-nav-text">参数默认值（Default parameters）</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%89%A9%E4%BD%99%E5%8F%82%E6%95%B0"><span class="toc-nav-text">剩余参数</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%B1%95%E5%BC%80%E6%95%B0%E7%BB%84-Spread"><span class="toc-nav-text">展开数组 Spread</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0%EF%BC%88Arrow-functions%EF%BC%89"><span class="toc-nav-text">箭头函数（Arrow functions）</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%AF%B9%E8%B1%A1%E5%AD%97%E9%9D%A2%E9%87%8F%E5%A2%9E%E5%BC%BA%EF%BC%88Enhanced-object-literals%EF%BC%89"><span class="toc-nav-text">对象字面量增强（Enhanced object literals）</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%AF%B9%E8%B1%A1%E6%89%A9%E5%B1%95%E6%96%B9%E6%B3%95"><span class="toc-nav-text">对象扩展方法</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#object-assign"><span class="toc-nav-text">object.assign</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#Object-is"><span class="toc-nav-text">Object.is()</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#Proxy-%E4%BB%A3%E7%90%86"><span class="toc-nav-text">Proxy 代理</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#Proxy-%E4%B8%8E-Object-defineProperty-%E6%AF%94%E8%BE%83"><span class="toc-nav-text">Proxy 与 Object.defineProperty 比较</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E5%80%9F%E5%8A%A9Proxy%E7%9B%91%E8%A7%86%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C"><span class="toc-nav-text">借助Proxy监视数组操作</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#Proxy-%E4%BB%A5%E9%9D%9E%E4%BE%B5%E5%85%A5%E7%9A%84%E6%96%B9%E5%BC%8F%E7%9B%91%E7%AE%A1%E5%AF%B9%E8%B1%A1%E8%AF%BB%E5%86%99"><span class="toc-nav-text">Proxy 以非侵入的方式监管对象读写</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#Reflect"><span class="toc-nav-text">Reflect</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E7%BB%9F%E4%B8%80%E6%93%8D%E4%BD%9C%E5%AF%B9%E8%B1%A1%E7%9A%84API"><span class="toc-nav-text">统一操作对象的API</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#Classes-%E7%B1%BB"><span class="toc-nav-text">Classes 类</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E9%9D%99%E6%80%81%E6%88%90%E5%91%98"><span class="toc-nav-text">静态成员</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#static%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-nav-text">static关键字</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E7%BB%A7%E6%89%BF-extends"><span class="toc-nav-text">继承 extends</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#Set%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="toc-nav-text">Set数据结构</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#Map-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%8C%E6%98%A0%E5%B0%84%E4%B8%A4%E4%B8%AA%E4%BB%BB%E6%84%8F%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB"><span class="toc-nav-text">Map 数据结构，映射两个任意类型数据之间的关系</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#Symbol-%E7%AC%AC%E4%B8%83%E4%B8%AA%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%EF%BC%8C-esmascript-2017-%E7%AC%AC%E5%85%AB%E4%B8%AA%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8BBigInt"><span class="toc-nav-text">Symbol  第七个数据类型，(esmascript 2017 第八个数据类型BigInt)</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#for-of-%E5%BE%AA%E7%8E%AF%EF%BC%8C"><span class="toc-nav-text">for of 循环，</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%A8%A1%E5%BC%8F-%E8%AE%A9%E7%94%A8%E6%88%B7%E9%80%9A%E8%BF%87%E7%89%B9%E5%AE%9A%E7%9A%84%E6%8E%A5%E5%8F%A3%E8%AE%BF%E9%97%AE%E5%AE%B9%E5%99%A8%E7%9A%84%E6%95%B0%E6%8D%AE%EF%BC%8C%E4%B8%8D%E9%9C%80%E8%A6%81%E4%BA%86%E8%A7%A3%E5%AE%B9%E5%99%A8%E5%86%85%E9%83%A8%E7%9A%84%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E3%80%82"><span class="toc-nav-text">迭代器模式 让用户通过特定的接口访问容器的数据，不需要了解容器内部的数据结构。</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E7%94%9F%E6%88%90%E5%99%A8-Generator"><span class="toc-nav-text">生成器 Generator</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E7%94%9F%E6%88%90%E5%99%A8%E5%BA%94%E7%94%A8"><span class="toc-nav-text">生成器应用</span></a></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#ES2016"><span class="toc-nav-text">ES2016</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95includs"><span class="toc-nav-text">数组方法includs</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E6%8C%87%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-nav-text">指数运算符</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#ES2017"><span class="toc-nav-text">ES2017</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'https://github.com/alongithub/2020/08/26/ES6/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', '/css/images/error_icon.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== '/css/images/error_icon.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>






    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    Copyright&copy; 2019-2022 along
    <br>
    <a href="https://beian.miit.gov.cn/" target="_blank">京ICP备2020044791号-1</a>
    <!-- <br>
    Theme by <a href="https://github.com/alongithub">alongithub</a>
     -->
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("/cdn.along/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("/cdn.along/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      hljs.configure({useBR: true});
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->


<script src="/js/script.js"></script>


  </body>
</html>